﻿using BellAssistant.Common.CommonHelper;
using BellAssistant.DSS;
using BellAssistant.Model.Tables;
using BellAssistant.Model.Views;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BellAssistant.DataManager
{
    public partial class CommissionManager : Form
    {
        public CommissionManager()
        {
            InitializeComponent();
            txtProfit.ReadOnly = true;
            btnSave.Click += btnSave_Click;
            this.Load += CommissionManager_Load;
            btnSearch.Click += new EventHandler(btnSearch_Click);
            btnExport.Click += btnExport_Click;


            txtPrice.KeyUp += TxtProfit_KeyUp;
            txtCost.KeyUp += TxtProfit_KeyUp;
            txtPromotionPrice.KeyUp += TxtProfit_KeyUp;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewHelper.ExportToExcel(gridView1, null);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog.Error(ex.Message);
            }
        }

        private void TxtProfit_KeyUp(object sender, KeyEventArgs e)
        {
            var price = 0d;
            var cost = 0d;
            var promotionPrice = 0d;
            var costFlag = string.IsNullOrEmpty(txtCost.Text) ? false : double.TryParse(txtCost.Text, out cost);
            var priceFlag = string.IsNullOrEmpty(txtPrice.Text) ? false : double.TryParse(txtPrice.Text, out price);
            var promotionPriceFlag = string.IsNullOrEmpty(txtPromotionPrice.Text) ? false : double.TryParse(txtPromotionPrice.Text, out promotionPrice);
            promotionPrice = promotionPrice == 0d ? price : promotionPrice;
            txtProfit.Text = (promotionPrice - cost).ToString();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            List<Goods> list = CreateDataSourceService.IDataSourceService.GetAllGoods();
            List<GoodsView> goodsListView = new List<GoodsView>();
            foreach (var item in list)
            {
                GoodsView temp = new GoodsView();
                List<ProductCategory> listCategory = CreateDataSourceService.IDataSourceService.GetAllProductCategory();
                temp.ProductCategoryName = listCategory.Find(s=>s.ProductID== item.ProductCategoryID).CategoryName;

                temp.Name = item.Name;

                List<Brand> listBrand = CreateDataSourceService.IDataSourceService.GetAllBrand();
                temp.BrandName= listBrand.Find(s => s.BrandID == item.BrandID).BrandName;

                temp.Source = item.Source;

                List<PromotionType> listType = CreateDataSourceService.IDataSourceService.GetAllPromotionType();
                temp.PromotionTypeName = listType.Find(s => s.PromotionID == item.PromotionID).PromotionName;

                List<ExpressType> expressTypeList = CreateDataSourceService.IDataSourceService.GetAllExpressType();
                temp.ExpressTypeName = expressTypeList.Find(s => s.ExpressTypeID == item.ExpressTypeID).ExpressTypeName;

                temp.Cost = item.Cost;
                item.Price = item.Price;
                temp.PromotionPrice = item.PromotionPrice;
                temp.Profit = item.Profit;
                temp.ConsignmentPlace = item.ConsignmentPlace;

                goodsListView.Add(temp);
            }
            gridControl1.DataSource = goodsListView;
        }

        private void CommissionManager_Load(object sender, EventArgs e)
        {
            #region 
            //商品类别
            List<ProductCategory> list = CreateDataSourceService.IDataSourceService.GetAllProductCategory();
            sleCategory.Properties.DataSource = null;
            List<ProductCategoryView> listView = new List<ProductCategoryView>();
            foreach (var item in list)
            {
                ProductCategoryView temp = new ProductCategoryView();
                //temp.ProductID = item.ProductID;
                temp.CategoryName = item.CategoryName;
                listView.Add(temp);
            }
            sleCategory.Properties.DataSource = listView;
            sleCategory.Properties.NullText = (listView != null && listView.Count > 0) ? "请选择类别" : "未查询到任何类别";

            //促销类型
            List<PromotionType> listType=CreateDataSourceService.IDataSourceService.GetAllPromotionType();
            List<PromotionTypeView> listTypeView = new List<PromotionTypeView>();
            foreach (var item in listType)
            {
                PromotionTypeView temp = new PromotionTypeView();
                //temp.PromotionID = item.PromotionID;
                temp.PromotionName = item.PromotionName;
                listTypeView.Add(temp);

                //cmbPromotionType.Properties.Items.Add(temp);
            }
            foreach (var item in listTypeView)
            {
                cmbPromotionType.Properties.Items.Add(item.PromotionName);
            }

            //品牌
            List<Brand> listBrand =  CreateDataSourceService.IDataSourceService.GetAllBrand();
            List<BrandView> listBrandView = new List<BrandView>();
            foreach (var item in listBrand)
            {
                BrandView temp = new BrandView();
                temp.BrandName = item.BrandName;
                listBrandView.Add(temp);
            }
            sleBrand.Properties.DataSource = listBrandView;
            sleBrand.Properties.NullText = (listView != null && listView.Count > 0) ? "请选择品牌" : "未查询到任何品牌";

            //快递类别
            List<ExpressType> expressTypeList  = CreateDataSourceService.IDataSourceService.GetAllExpressType();
            List<ExpressTypeView> expressTypeView = new List<ExpressTypeView>();
            foreach (var item in expressTypeList)
            {
                ExpressTypeView temp = new ExpressTypeView();
                temp.ExpressTypeName = item.ExpressTypeName;
                expressTypeView.Add(temp);
            }
            sleExpressType.Properties.DataSource = expressTypeView;
            sleExpressType.Properties.NullText = (listView != null && listView.Count > 0) ? "请选择快递类别" : "未查询到任何快递类别";

            //发货地
            foreach (var item in expressTypeList)
            {
                cmbFromPlace.Properties.Items.Add(item.ExpressFromAddress);
            }
            //促销日期---根据所选促销类型而定
            #endregion

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string flg = IsEmptyValue();
            if (string.IsNullOrEmpty(flg))
            {
                Goods good = new Goods();
                #region 
                good.ProductCategoryID = CreateDataSourceService.IDataSourceService.GetProductCategoryIDByName(sleCategory.Text.ToString());
                good.Source = cmbSource.SelectedText;
                good.URL = txtUrl.Text;

                good.Name = txtName.Text;
                good.PromotionID = CreateDataSourceService.IDataSourceService.GetPromotionIDByName(cmbPromotionType.Text);
                good.Cost = Convert.ToDouble(txtCost.Text);
                good.Price = Convert.ToDouble(txtPrice.Text);
                good.PromotionPrice = Convert.ToDouble(txtPromotionPrice.Text);
                good.StatusMean = cmbStatus.Text;
                good.Status = good.StatusMean == "上架" ? true : false;

                good.BrandID = CreateDataSourceService.IDataSourceService.GetBrandIDByName(sleBrand.Text);
                good.ExpressTypeID = CreateDataSourceService.IDataSourceService.GetExpressTypeIDByName(sleExpressType.Text);
                good.Profit = Convert.ToDouble(txtProfit.Text);
                good.Stock = Convert.ToInt32(txtStock.Text);
                good.ConsignmentPlace = cmbFromPlace.Text;

                good.PromotionStart = promotionStart.DateTime;
                good.PromotionEnd = promotionEnd.DateTime;
                good.Shelves = DateTime.Now;
                good.OffShelves = DateTime.Now.AddDays(7);

                //合作快递
                string str = "";
                for (int i = 0; i < checkbExpress.CheckedIndices.Count; i++)
                {
                    str += checkbExpress.Items[checkbExpress.CheckedIndices[i]].ToString() + "|";
                }
                good.ExpressChipper = str;
                #endregion
                bool saveFlg = CreateDataSourceService.IDataSourceService.SaveGoods(good);
                if (saveFlg)
                {
                    MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show("保存失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                btnSearch_Click(sender, e);

            }
            else
            {
                MessageBox.Show(flg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }
        private string IsEmptyValue()
        {
            string str = "";
            #region
            if (string.IsNullOrEmpty(sleCategory.Text) || cmbSource.Text == "" || txtUrl.Text == "" || txtName.Text == "" || string.IsNullOrEmpty(cmbPromotionType.Text))
            {
                str = "请填写完整!";
            }
            if (string.IsNullOrEmpty(cmbPromotionType.Text) || txtCost.Text == "" || txtPrice.Text == "" || txtPromotionPrice.Text == "" || string.IsNullOrEmpty(cmbStatus.Text))
            {
                str = "请填写完整!";
            }
            if (string.IsNullOrEmpty(sleBrand.Text) || string.IsNullOrEmpty(sleExpressType.Text) || txtProfit.Text == "" || txtStock.Text == "" || string.IsNullOrEmpty(cmbFromPlace.Text))
            {
                str = "请填写完整!";
            }
            if (string.IsNullOrEmpty(promotionStart.Text) || string.IsNullOrEmpty(promotionEnd.Text) || string.IsNullOrEmpty(shelvesStart.Text))
            {
                str = "请填写完整!";
            }
            //checkbExpress.CheckedIndices[i];
            if (checkbExpress.CheckedItems.Count == 0)
            {
                str = "请填写完整!";
            }
            #endregion
            return str;
        }

    }
}
